package com.utils.picTxt.pic
{
	import com.org.gif.player.GIFPlayer;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	public class FaceGather extends Sprite
	{
		private static const PREFIX_TAG:String = 'assets/images/face/';
		private static const SUFFIX_PNG_TAG:String = '.png';
		/**表情尺寸*/		
		public static const FACE_SIZE:int = 24;
		/**表情数量*/		
		private const FACE_COUNT:int = 34;
		
		private var _faceItem:PicItem;
		
		/**返回表情Class*/		
		private var _returnTipTarget:Function = new Function();
		
		/**表情数组 表情:1-32:face_(01-32)  性别标记:33:男 34:女*/
		private static var _faceList:Array 
		= [
			Face_0,Face_1,Face_2,Face_3,Face_4,Face_5,Face_6,Face_7,Face_8,Face_9,Face_10,
			Face_11,Face_12,Face_13,Face_14,Face_15,Face_16,Face_17,Face_18,Face_19,Face_20,
			Face_21,Face_22,Face_23,Face_24,Face_25,Face_26,Face_27,Face_28,Face_29,Face_30,
			Face_31,Face_32,Face_nan,Face_nv
		  ];
		
		/**表情bitmap*/		
		private var _faceContainer:Sprite;
		
		public function FaceGather()
		{
			initContainer();
			initItem();
			initBg();
			if(this.hasEventListener(Event.REMOVED_FROM_STAGE)){
				this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveToStage);
			}
		}
		
		/**清理函数*/		
		private function onRemoveToStage(e:Event):void
		{
			_faceContainer.removeEventListener(MouseEvent.CLICK,onClickHandler);
			this.removeEventListener(Event.REMOVED_FROM_STAGE,onRemoveToStage);
			while(this.numChildren) removeChildAt(0);
			_faceContainer = null;
		}
		
		/**绘制背景*/		
		private function initBg():void
		{
			this.graphics.clear();
			this.graphics.lineStyle(2,0xa39271);
			this.graphics.beginFill(0,.7);
			this.graphics.drawRoundRect(-5,-5,_faceContainer.width+10,_faceContainer.height+10,10,10);
			this.graphics.endFill();
		}
		
		/**初始化容器*/		
		private function initContainer():void
		{
			if(!_faceContainer) _faceContainer = new Sprite();
			addChild(_faceContainer);
		}
		
		/**添加表情 */		
		private function initItem():void
		{
			for (var i:int = 0; i <= FACE_COUNT-2; i++) 
			{
				_faceItem = new PicItem(_faceList[i]);
				_faceItem.x = (i%7) * FACE_SIZE;
				_faceItem.y = int(i/7) * FACE_SIZE;
				_faceContainer.addChild(_faceItem);
			}
			_faceContainer.buttonMode = true;
			_faceContainer.addEventListener(MouseEvent.CLICK,onClickHandler);
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			var clsName:String = String((e.target.toString()).split(' ')[1]).split(']')[0]
			trace(clsName);
			var clsIndex:int = int(clsName.split('_')[1]);
			_returnTipTarget(_faceList[clsIndex]);//回调Class
		}		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		public static function get faceList():Array{return _faceList;}
		
		/**返回所选表情的名称*/		
		public function set returnTipTarget(value:Function):void{_returnTipTarget = value;}
		
	}
}